<?php
/**
 * 
 * @author tangbl
 * 
 */
class UplaodAction extends CommonAction {
	//删除图片
	public function del(){
		
	}
	
	//裁剪图片
	public function crop(){	
		$pic_url = $this->_get('pic_url');
		$x = $this->_get('x','intval',0);
		$y = $this->_get('y','intval',0);
		$sw = $this->_get('sw');
		$w = $this->_get('w','intval',0);
		$h = $this->_get('h','intval',0);
		
		
		$copperImage = "";
		
		$image_info = getimagesize('http://'.$_SERVER['HTTP_HOST'].$pic_url);
		$scaling = $image_info[0]/$sw;		
		
		$x = floor($x*$scaling);
		$y = floor($y*$scaling);
		$w = floor($w*$scaling);
		$h = floor($h*$scaling);
		
		if($image_info){
			//要根据返回的不同类型图片来创建resource
			if($image_info[2]==1){
				$rs_image = imagecreatefromgif($_SERVER['DOCUMENT_ROOT'].$pic_url);
				$copperImage = str_replace(".gif","_".$x."_".$y."_".$w."_".$h.".gif",$pic_url);
			}elseif($image_info[2]==2){
				$rs_image = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'].$pic_url);
				$copperImage = str_replace(".jpg","_".$x."_".$y."_".$w."_".$h.".jpg",$pic_url);
			}elseif($image_info[2]==3){
				$rs_image = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'].$pic_url);
				$copperImage = str_replace(".png","_".$x."_".$y."_".$w."_".$h.".png",$pic_url);
			}else{
				$rs_image = false;
			}
			if($rs_image){
				
				$dst_image=imagecreatetruecolor($w,$h);	//创建新图像
				imagecopy($dst_image,$rs_image,0,0,$x,$y,$w,$h);
				imagejpeg($dst_image,$_SERVER['DOCUMENT_ROOT'].$copperImage);		//将新图像保存为$copperImage
				imagedestroy($dst_image);	
				imagedestroy($rs_image);
				
				$arr = array(
					'crop' => 'ok',
					'url' => $copperImage,
					'tis' => L('upl_tis_1'),
				);			
			}else{
				$arr = array(
					'crop' => 'no',
					'url' => '',
					'tis' => L('upl_tis_2'),
				);
			}
		}else{
			$arr = array(
				'crop' => 'no',
				'url' => '',
				'tis' => L('upl_tis_3'),
			);
		}
	
		$this->ajaxReturn($arr);
	}
}
?>